import openpyxl
import xlrd
import time
import pandas as pd
import datetime


# maxnrows=100

# 获取毒物所有症状
def read_title():
    lst_title = []
    wk = openpyxl.load_workbook('../data/duwuzhengzhuang.xlsx')
    sheet = wk.active
    rows = sheet.max_row
    cols = sheet.max_column
    for i in range(2, rows + 1):
        for j in range(1, cols + 1):
            size = sheet.cell(i, j).value
            lst_title.append(size)
    new_title = [x for x in lst_title if x != None]
    return new_title


# 将文字进行分词，提取关键字
def get_word(str):
    lst_word = []
    for item in lst_title:
        if (str.find(item) != -1):
            lst_word.append(item)
    return lst_word


name_lst = []
content_lst = []


def get_name():
    # 毒物症状描述
    wk = xlrd.open_workbook('../data/duwuxiangqing.xls')
    table = wk.sheet_by_name('Sheet1')
    rows = table.nrows

    for i in range(2, rows):
        name = table.cell(i, 2).value
        str = table.cell(i, 17).value + table.cell(i, 18).value
        content_lst.append(str)
        name_lst.append(name)
    return None


# 得到毒物对应的症状关键字
def get_posion(lst):
    # 结果集合，第一个是毒物名称，剩余为毒物对应的症状
    ans_lst = []
    for item in name_lst:
        k = []
        k.append(item)
        ans_lst.append(k)
    for item in lst:
        i = 0
        for str in content_lst:
            if (str.find(item) >= 0):
                ans_lst[i].append(item)
            i += 1
    return ans_lst


if __name__ == '__main__':

    # 计算运行时间
    starttime = datetime.datetime.now()

    lst_title = read_title()
    # name:name_lst
    get_name()

    # 2.1获取毒物描述对应的关键字
    # 这个重复读入，导致速度变慢
    lst_posion = get_posion(lst_title)

    first = 0
    five = 0
    nosimple = 0

    fives = []
    nofives = []

#----------------------------------------新增权值词频统计---------------------------------------------------------
    # 权值词统计
    words = {}
    for item in lst_posion:
        for i in range(len(item)):
            if i != 0:
                if not item[i] in words:
                    words[item[i]] = 1
                else:
                    words[item[i]] = words[item[i]] + 1
    wordsans = sorted(words.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)
    maxn = wordsans[0][1] + 10

    newwords = {}
    for item in wordsans:
        newwords[item[0]] = round((1 - item[1] / maxn) * 10, 2)
# ----------------------------------------新增权值词频统计---------------------------------------------------------

    print("毒物总数：" + str(len(name_lst)))
    for i in range(len(name_lst)):
        print("目前个数：", i)
        strname = name_lst[i]

        strdesprition = content_lst[i]
        # 1进行分词
        lst_word = get_word(strdesprition)

        # 2.2构建毒物匹对分词的个数
        temp = {}
        # 2.3开始匹对

        for item in lst_posion:

            if len(item) > 1:
                k = 0.0
                for sample in item:
                    for st in lst_word:
                        if sample == st:
                            k = k + newwords[sample]
                            break
                names = item[0]
                # 判断selflen要小于等于lst_word的原因是：目标毒物一定就有：len(lst_word)个特征，
                # 所以待检测的毒物特征若是超过len(lst_word)个，那么代表它肯定不是目标毒物
                selflen = len(item) - 1
                if ((names in temp.keys()) == False & selflen <= len(lst_word)):
                    temp[names] = k
                elif temp[names] < k and selflen <= len(lst_word):
                    temp[names] = k
        ans = sorted(temp.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)

        l = 1
        for item in ans:
            if item[0] == strname:
                if l == 1:
                    first = first + 1
                    # print("匹配上Top1："+item[0])
                elif l <= 5:
                    five = five + 1
                    # print("匹配上Top5：" + item[0])
                elif l > 5:
                    # print("匹配上Top5之外：" + item[0])
                    fives.append(item[0])
                    # print(strname)
                break
            l = l + 1
        if l == len(ans) + 1:
            # print("未匹对上："+strname)
            nofives.append(strname)
            nosimple = nosimple + 1

    print("Top5之外的毒物有：" + str(fives))
    print("完全未匹对上的毒物有：" + str(nofives))
    print("Top1的有：" + str(first) + " Top5的有：" + str(five + first) + " Top5之外的有：" + str(len(fives)) + " 完全未匹对有：" + str(
        len(nofives)))
    sum = first + five + len(fives)
    print("Top1准确率：" + str(first * 1.0 / sum))
    print("Top5准确率：" + str((five + first) * 1.0 / sum))

    endtime = datetime.datetime.now()

    print("总运行时间：", str((endtime - starttime).seconds) + "秒")

#之前的没加权值
# Top1的有：874 Top5的有：1019 Top5之外的有：195 完全未匹对有：808
# Top1准确率：0.7199341021416804
# Top5准确率：0.8393739703459637


#加入权值后的：提升了Top5进入Top1的概率，而Top5之外的
# Top1的有：876 Top5的有：1019 Top5之外的有：195 完全未匹对有：808
# Top1准确率：0.7215815485996705
# Top5准确率：0.8393739703459637
